package yu.com.time

import yu.com.time.calclator.LocalDateCalculator
import yu.com.time.unit.DateUnit
import java.time.LocalDate


/**
 * 对jdk8日期进行拓展
 * created zpf on 2018/10/23 0023
 */


/**
 * 转换到普通Date
 */
fun LocalDate.toDate(): java.sql.Date = java.sql.Date.valueOf(this)

/**
 * 设置时间部分
 * @param year 设置小时部分,默认置为0，如果填写null则不改变
 * @param month 设置分钟部分,默认置为0，如果填写null则不改变
 * @param day 设置秒部分,默认置为0，如果填写null则不改变
 */
fun LocalDate.withDate(year: Int? = 0, month: Int? = 0, day: Int? = 0): LocalDate {
    var time = this
    year?.let { time = time.withYear(it) }
    month?.let { time = time.withMonth(it) }
    day?.let { time = time.withDayOfMonth(it) }
    return time
}


/**年份计算*/
val LocalDate.cYear get() = LocalDateCalculator(this, DateUnit.YEAR)

/**月份计算*/
val LocalDate.cMonth get() = LocalDateCalculator(this, DateUnit.MONTH)

/**星期计算*/
val LocalDate.cWeek get() = LocalDateCalculator(this, DateUnit.WEEK)

/**天数计算*/
val LocalDate.cDay get() = LocalDateCalculator(this, DateUnit.DAY)